package com.android.view.fragment.markets;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.presenter.iview.ISymbolListView;
import com.android.view.activity.quote.MarketInfoActivity;
import com.facebook.drawee.view.SimpleDraweeView;
import com.android.R;
import com.android.model.entity.res.SymbolListResult;
import com.android.presenter.info.SymbolInfoPresenter;
import com.android.view.activity.trade.TradeActivity;
import com.android.view.fragment.base.BaseFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindColor;
import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * author:  TenFace
 * date:    2017/9/27
 * description:
 */

public class BTCFragment extends BaseFragment implements ISymbolListView {


    @BindColor(R.color.lite_blue)
    int colorBlue;
    @BindColor(R.color.green)
    int colorGreen;
    @BindColor(R.color.orange)
    int colorOrange;

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private SymbolInfoPresenter symbolInfoPresenter;

    public static BTCFragment newInstance(int type) {
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        BTCFragment fragment = new BTCFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int setContentLayout() {
        return R.layout.fragment_ex55;
    }

    @Override
    protected void initView() {
        //设置下拉刷新的圆环变化颜色，蓝绿橙
//        mSrlMovie.setColorSchemeColors(colorBlue, colorGreen, colorOrange);
        symbolInfoPresenter = new SymbolInfoPresenter(this);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);

    }

    @Override
    protected void obtainData() {
        symbolInfoPresenter.getSymbolList();
//        String result = FileUtil.getJson("55T.json", getContext());
//        Ex55Result ex55Result = JsonUtil.parseJsonToBean(result, Ex55Result.class);
//        recyclerView.setAdapter(new Ex55Adapter(getContext(), ex55Result.getData()));
    }

    @Override
    protected void initEvent() {

    }

    @Override
    public void getSymbolListSuccess(List<SymbolListResult> symbolListResultList) {
        List<SymbolListResult> newResult = new ArrayList<>();
        for (int i = 0; i < symbolListResultList.size(); i++) {
            if (symbolListResultList.get(i).getQuoteAsset().equals("BTC")){
                newResult.add(symbolListResultList.get(i));
            }
        }
        recyclerView.setAdapter(new Ex55Adapter(getContext(), newResult));
    }

    @Override
    public void getSymbolListFail(int status, String desc) {

    }


    public class Ex55Adapter extends RecyclerView.Adapter<Ex55Adapter.Ex55Holder> {

        private Context mContext;
        private List<SymbolListResult> results;

        public Ex55Adapter(Context context, List<SymbolListResult> symbolListResultList) {
            super();
            this.mContext = context;
            this.results = symbolListResultList;
        }

        @Override
        public Ex55Holder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_markets, null);
            Ex55Adapter.Ex55Holder ex55Holder = new Ex55Adapter.Ex55Holder(view);
            return ex55Holder;
        }

        @Override
        public void onBindViewHolder(final Ex55Adapter.Ex55Holder holder, int position) {
            holder.tvName.setText(results.get(position).getBaseAsset());
//            holder.tvVol.setText(results.get(position).getVolume());
//            holder.tvPrice.setText(results.get(position).getPrice());
//            holder.tvPriceCNY.setText("¥" + results.get(position).getCny());
//
            if (position == 1 || position == 2 || position == 3 || position == 5 || position == 8 || position == 13) {
                holder.sdvLogo.setImageResource(R.mipmap.btc_logo);
            } else {
                holder.sdvLogo.setImageResource(R.mipmap.eth_logo);
            }
//
//
//            if (results.get(position).getChange() > 0) {
//                holder.tvPrice.setTextColor(getResources().getColor(R.color.main_green));
//                holder.llChangeTxt.setBackgroundDrawable(getResourceDrawable(R.drawable.quote_stock_green));
//                holder.tvChange.setText("+" + results.get(position).getChange() + "%");
//            } else if (results.get(position).getChange() < 0) {
//                holder.tvPrice.setTextColor(getResources().getColor(R.color.main_red));
//                holder.llChangeTxt.setBackgroundDrawable(getResourceDrawable(R.drawable.quote_stock_red));
//                holder.tvChange.setText(results.get(position).getChange() + "%");
//            } else {
//                holder.tvPrice.setTextColor(getResources().getColor(R.color.main_black));
//                holder.llChangeTxt.setBackgroundDrawable(getResourceDrawable(R.drawable.quote_stock_zero));
//                holder.tvChange.setText(results.get(position).getChange() + "%");
//            }

            holder.quoteInfoItem.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
//                    TradeActivity.actionStart(getContext(), holder.tvName.getText().toString(), "BTC");
                    MarketInfoActivity.actionStart(getContext(), holder.tvName.getText().toString(), "BTC");
                }
            });

        }

        @Override
        public int getItemCount() {
            return results.size();
        }

        class Ex55Holder extends RecyclerView.ViewHolder {

            @BindView(R.id.tvName)
            TextView tvName;
            @BindView(R.id.tvVol)
            TextView tvVol;
            @BindView(R.id.tvPrice)
            TextView tvPrice;
            @BindView(R.id.tvPriceCNY)
            TextView tvPriceCNY;
            @BindView(R.id.tvChange)
            TextView tvChange;
            @BindView(R.id.llChangeTxt)
            LinearLayout llChangeTxt;
            @BindView(R.id.quoteInfoItem)
            LinearLayout quoteInfoItem;
            @BindView(R.id.sdvLogo)
            SimpleDraweeView sdvLogo;

            public Ex55Holder(View itemView) {
                super(itemView);
                ButterKnife.bind(this, itemView);
            }
        }
    }


}
